using Microsoft.AspNetCore.Http;
using Vin.Extension.Exception.Interface;
using Vin.Tool.AspNetCore.Singleton;
using Vin.Tool.Domain.ExceptionModel;

namespace Vin.Extension.Exception.Models;

public class NotifyException : ErrorException
{
    private List<IExceptionNotifyHandler> _handlers =>
        VinApp.GetServicesByHttpContext<IExceptionNotifyHandler>().ToList();

    public NotifyException(string? msg = "") : base(StatusCodes.Status500InternalServerError, msg)
    {
        Notify().Wait();
    }

    public NotifyException(int code, string? msg = "") : base(code, msg)
    {
        Notify().Wait();
    }

    public Task Notify()
    {
        var taskList = new List<Task>();
        foreach (var handler in _handlers)
        {
            taskList.Add(handler.NotifyAsync(this, this.Code, this.Message));
        }

        return Task.WhenAll(taskList);
    }
}